package com.cjlgb.design.common.security.util;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @author WFT
 * @date 2019/7/26
 * description:
 */
public class SpringSecurityUtil {

    /**
     * @return 当前用户的主体信息
     */
    public static Authentication getAuthentication(){
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * @return 当前用户的登录名称
     */
    public static String getLoginName(){
        return SpringSecurityUtil.getAuthentication().getName();
    }

    /**
     * @return 当前用户的权限标识列表
     */
    public static Collection<String> getAuthorities(){
        return getAuthentication().getAuthorities().stream()
                .map(GrantedAuthority::getAuthority).collect(Collectors.toList());
    }
}
